﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using WebAppPlanProduccion.Code.Security;
using Web.Framework;
using PlanProd.Contracts.Search;
using PlanProd.Contracts;

namespace WebAppPlanProduccion.App
{
    public partial class ReportServerList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.ADMINISTRAR_REPORTSERVER);
            if (!this.IsPostBack)
            {
                Buscar();
            }
        }

        protected override void ApplySecurity()
        {
        }

        private void Buscar()
        {
            SearchReportServer search = new SearchReportServer();
            search.Url = this.txtNombre.Text;
            
            var reportes = WebServiceHelper.GetReportServers(search);

            this.Grid.DataSource = reportes;
            this.Grid.DataBind();

            pnlResults.Visible = true;
        }

        protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[0].Attributes.Add("title", "Editar");
                    e.Row.Cells[1].Attributes.Add("title", "Eliminar");
                    ImageButton btnEliminarCtrl = (ImageButton)e.Row.FindControl("btnEliminar");
                    btnEliminarCtrl.OnClientClick = string.Format("return confirm('Está seguro que desea eliminar el siguiente registro? {0}')", ((ReportServer)e.Row.DataItem).Url);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int reportId = Convert.ToInt32(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format("/App/ReportServerEdit.aspx?Id={0}", reportId), true);
                }
                else if (e.CommandName == "eliminar")
                {
                    int reportId = Convert.ToInt32(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.EliminarReportServer(reportId);
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.Grid.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void btnAgregarNuevo_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("/App/ReportServerEdit.aspx", true);
        }
    }
}